package api

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"github.com/astaxie/beego"
	"strconv"
)

type AccessControlController struct {
	BaseController
}

// 家长门禁通知
func (c *AccessControlController) ListByParent() {
	var res *utils.ResJsonStruct

	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}

	userId := c.GetSession("miniUserId")
	p := map[string]interface{}{}
	p["id"] = userId
	user := service.GetUserInfo(p)

	var studentIds []int64
	p = map[string]interface{}{}
	p["status"] = 1
	p["familySn"] = user.FamilySn
	_, studentList := service.GetStudentList(p)
	for _, student := range studentList {
		studentIds = append(studentIds, student.Id)
	}

	var accessControlList []*models.SafeNotice
	if len(studentIds) > 0 {
		p = map[string]interface{}{}
		p["studentIds"] = studentIds
		p["relation"] = true
		p["api"] = true
		p["curPage"] = curPage
		p["perCount"] = count
		_, accessControlList = service.GetSafeNoticeList(p)
	}

	res = utils.ResJson(0, "成功获取数据", accessControlList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 老师门禁历史
func (c *AccessControlController) ClassStudents() {
	var res *utils.ResJsonStruct

	p := map[string]interface{}{}
	p["userId"] = c.GetSession("miniUserId")
	p["apiWithStudent"] = true
	_, classList := service.GetClassList(p)

	res = utils.ResJson(0, "成功获取数据", classList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 老师门禁历史列表
func (c *AccessControlController) ListByTeacher() {
	var res *utils.ResJsonStruct
	studentId := c.GetString("id")

	if len(studentId) <= 0 {
		res = utils.ResJson(1300, "学生Id异常", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}

	p := map[string]interface{}{}
	p["studentId"] = studentId
	p["relation"] = true
	p["api"] = true
	p["curPage"] = curPage
	p["perCount"] = count
	_, accessControlList := service.GetSafeNoticeList(p)

	res = utils.ResJson(0, "成功获取数据", accessControlList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}